var api = require("../../api/api.js");
var storageManage = require("../../manage/storageManage.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");
var imgUp = require("../../template/imgUp/imgUp.js");

Component({
  options: {
    multipleSlots: false // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    commodity_cost: String,
    taptype: String
  },
  data: {
    user_type:"",
    animationData: "",
    showModalStatus: false,
    windowHeight: '',
    maxLength: 3,
    balance: 0,
    imglist: [],
    isAddBtnShow: true,
    paymentState: 0,
    xxIsShow: false,
    shIsShow: false,
  },
  methods: {
    //展示弹框
    showModalContainer(data) {
      var that = this;
      imgUp.init.apply(this, []);//引入方法
      wx.getSystemInfo({
        success: function (res) {
          that.data.windowHeight = res.windowHeight;
        }
      });
      this.setData({
        user_type:storageManage.getGlobalConfig().user_type,
        commodity_cost: data.commodity_cost,
        balance: data.balance ? data.balance : storageManage.getGlobalConfig().balance,
        order_no: data.order_no

      });
      that.animationModal(true)
    },
    // 隐藏遮罩层
    hideModal: function () {
      this.animationModal(false)
    },
    //选择支付方式
    select: function (e) {
      let state = e.currentTarget.dataset.state;
      let xxIsShow = false;
      let shIsShow = false;

      if (state == 1) {
        xxIsShow = true;
      } else {
        xxIsShow = false
      }
      if (state == 2) {
        shIsShow = true;
      } else {
        shIsShow = false
      }
      if (state == 3) {
        if (this.data.balance < this.data.commodity_cost) {
          return;
        }
      }
      this.setData({
        paymentState: state,
        xxIsShow: xxIsShow,
        shIsShow: shIsShow
      })
    },
    /**
     * pay in advance
     */
    payTap: utils.throttle(function (e) {
      var that = this;
      var paymentState = this.data.paymentState;
      var payload = {
        order_no: that.data.order_no,
        pay_type: parseInt(paymentState)
      }
      //线下支付
      if (paymentState == 1) {
        if (that.data.imglist.length <= 0) {
          toastShow("最少上传一张凭证");
          return;
        }
        payload.images = that.data.imglist;
      } else if (paymentState == 2) { //赊货 

        payload.on_creadit = that.data.creadit;
      }
      if (this.data.payTapState) { //true是正在进行支付动作
        return
      }
      that.setData({
        payTapState: true
      })

      api.default.api('check_pay_type', payload, function (data) {

        var paymentState = that.data.paymentState;
        if (paymentState == 0) {
          utils.payTap(that, that.data.order_no, function (data) { //已付款
            that.data.payTapState = false;
            that.triggerEvent('pay', { paymentState: paymentState }, {})
          }, function (e) {
            //支付失败
            that.data.payTapState = false;
            toastShow(e.err_desc)
            that.pay_fail(res.data.order_no) //通知服务器支付失败
          })
        } else {
          if (that.data.paymentState==3){
            let balance = (that.data.balance * 100 - that.data.commodity_cost*100)/10000;
            let newConfig = storageManage.getGlobalConfig();
            newConfig.balance = balance;
            storageManage.setGlobalConfig(newConfig);
          }
          that.triggerEvent('pay', { paymentState: paymentState }, {})
        }



      }, function () { });
    }),
    //通知服务器支付失败
    pay_fail: function (order_no) {
      api.default.api('pay_fail', {
        "order_no": order_no
      }, function (data) {
        console.log(data)
      }, function () {

      })
    },
    //显示动画
    animationModal: function (state) {
      var animation = wx.createAnimation({
        duration: 50,
        timingFunction: "linear",
        delay: 0
      })
      animation.translateY(this.data.windowHeight).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: state
      })
      setTimeout(function () {
        animation.translateY(0).step();
        this.setData({
          animationData: animation.export()
        })
      }.bind(this), 300);
    },
    //留言
    remarkChange: function (e) {
      this.setData({
        "remark": e.detail.value
      });
    },
    //赊货备注
    creaditChange: function (e) {
      this.setData({
        "creadit": e.detail.value
      });
    },
  }
})
function toastShow(text, state) {
  wx.showToast({
    title: text,
    icon: state ? state : 'none',
    duration: 1000
  })
}